//
//  DescriptionShowView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/29.
//

#import "DescriptionShowView.h"


@interface DescriptionShowView ()<UITextViewDelegate>

@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,assign) CGFloat viewH;
@property (nonatomic ,strong) UIButton * rightbtn;
@property (nonatomic ,strong) UILabel * zsCount;
@property (nonatomic ,strong) IQTextView * textview;
@property (nonatomic ,strong) NSString * content;

@property (nonatomic ,strong) void(^block)(NSString * str);

@end
@implementation DescriptionShowView


+ (void)showViewWithTitle:(NSString *)title andContent:(NSString *)content andBlock:(void(^)(NSString * str))block{

DescriptionShowView * view = [DescriptionShowView new];
    [[DTKTool getCurrentVC].view addSubview:view];
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    view.viewH = kwt(340) + KSafeAreaBottomHeight;
    view.block = block;
    view.content = content;
    UIView * makeView = [UIView new];
    view.makeView = makeView;
    makeView.backgroundColor = KcolorHex(@"#F3F6F9");
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, view.viewH);
    [view addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH, view.viewH)];
    
    //    view.userInteractionEnabled = YES;
    //    [view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(dismiss)]];
    makeView.userInteractionEnabled = YES;
    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(noclick)]];
//    view.userInteractionEnabled = YES;
//    [view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(dismiss)]];
    
    UILabel * titlab = [DTKTool labelWithText:title andFont:kCFont(18) andTextColor:KcolorHex(@"#222222")];
    [makeView addSubview:titlab];
    [titlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.top.offset(kwt(15));
    }];
    
    
    UIButton * btn = [UIButton buttonWithType:0];
    [btn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    btn.addTarget(view,@selector(dismiss));
    [makeView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.width.height.offset(kwt(20));
        make.centerY.equalTo(titlab);
    }];
    
    UIView * buttonview = [UIView new];
    [makeView addSubview:buttonview];
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(60) + KSafeAreaBottomHeight);
    }];
    
    UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(355), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")];
    [qrbtn setTitle:@"确认" forState:UIControlStateNormal];
    [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    qrbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qrbtn];
    [qrbtn addTarget:view action:@selector(rightbtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qrbtn];
    [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(48));
        make.width.offset(kwt(355));
        make.centerX.offset(0);
        make.top.offset(kwt(0));
    }];
    
    UIView * pickermakeview = [UIView new];
    [makeView addSubview:pickermakeview];
    pickermakeview.backgroundColor = UIColor.whiteColor;
    [DTKTool YJBK:kwt(5) andView:pickermakeview andWidth:0.5 andColor:KcolorHex(@"#DBDFE4")];
    [pickermakeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.top.offset(kwt(47));
        make.bottom.equalTo(buttonview.mas_top).offset(kwt(-26));
    }];
    
    UILabel * btxlab = [DTKTool labelWithText:@"*" andFont:kCFont(17) andTextColor:KcolorHex(@"#FA0F0F")];
    [pickermakeview addSubview:btxlab];
    [btxlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(18));
        make.top.offset(kwt(15));
        make.height.offset(kwt(17));
    }];
    UILabel * btlab = [DTKTool labelWithText:content andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    [pickermakeview addSubview:btlab];
    [btlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btxlab.mas_right);
        make.centerY.equalTo(btxlab);
    }];
    
    
    UILabel * zsCount = [DTKTool labelWithText:@"0/40" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    view.zsCount = zsCount;
    [pickermakeview addSubview:zsCount];
    [zsCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-18));
        make.centerY.equalTo(btxlab);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#E7E7E7");
    [pickermakeview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(0.5);
        make.top.offset(kwt(44));
        make.centerX.offset(0);
        make.width.offset(kwt(320));
    }];
    
    IQTextView * textview = [IQTextView new];
    textview.textContainer.lineFragmentPadding = 0;
    textview.textColor = KcolorHex(@"#222222");
    textview.font = kFont(17);
    view.textview = textview;
    textview.placeholder = [NSString stringWithFormat: @"请填写%@",content];
    textview.delegate = view;
    textview.backgroundColor = UIColor.clearColor;
    [pickermakeview addSubview:textview];
    [textview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.top.offset(kwt(50));
        make.bottom.offset(kwt(-10));
    }];
    
    [view show];
    
    
}

- (void)rightbtnclick{
    if(self.textview.text.length < 1){
        [self showWarning:[NSString stringWithFormat: @"请填写%@",self.content]];
         return;
    }
    if(self.block){
        self.block(self.textview.text);
    }
    [self dismiss];
}

-(void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length > 40){
        textView.text = [textView.text substringToIndex:40];
    }
    self.zsCount.text = [NSString stringWithFormat:@"%lu/40",(unsigned long)textView.text.length];
}
- (void)show{
    [[DTKTool getCurrentVC].view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - self.viewH , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
    }];
}
- (void)noclick{}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}


@end
